import { createRouter, createWebHashHistory } from 'vue-router'
import NProgress from 'nprogress'
import config from '@/config/index.js'
import tools from "@/utils/tools.js";
import routes from './routes.js'

var router = createRouter({
    history: createWebHashHistory(),
    routes: routes
})

// 设置浏览器TITLE
document.title = config.APP_NAME

/**
 * 前置导航守卫
 */
router.beforeEach((to, from, next) => {
    NProgress.start()
    //动态标题
    document.title = to.meta.title ? `${to.meta.title} - ${config.APP_NAME}` : config.APP_NAME

    if(to.path === '/login'){
        next();
        return;
    }

    const token = tools.data.get(config.authentication.TOKEN_KEY)
    if(!token){
        next({
            path: '/login'
        });
        return false;
    }

    // 获取routers
    initRouter()

    next()
})

/**
 * 后置导航守卫
 */
router.afterEach((to, from, failure) => {
    NProgress.done();
})

const initRouter = () => {
    const menus = tools.data.get(config.system.MENU_KEY)



}

export default router